import { useEffect, useState } from 'react';
import { App } from 'antd';
import { history } from '@umijs/max';

export const useHistoryBlock = () => {
  const { modal } = App.useApp();
  const [unblock, setUnblock] = useState<Function | null>(null);
  useEffect(() => {
    const f = history.block((e) => {
      modal.confirm({
        title: '提示',
        content: '系统可能不会保存您所做的更改，确定要离开吗？',
        centered: true,
        onOk: () => {
          f();
          e.retry();
        },
      });
    });
    setUnblock(() => f);
    return () => f();
  }, []);
  return { unblock };
};
